﻿using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class InboundOrderUpdateHander : IRequestHandler<InboundOrderUpdateCommand, Result>
    {

        private readonly IBaseRepositorys<InboundOrder> inboundOrderRepository;

        public InboundOrderUpdateHander(IBaseRepositorys<InboundOrder> inboundOrderRepository)
        {
            this.inboundOrderRepository = inboundOrderRepository;
        }

        /// <summary>
        /// 修改入库单
        /// </summary>
        /// <param name="request">修改的入库单信息</param>
        /// <param name="cancellationToken">允许取消异步</param>
        /// <returns>返回影响行数</returns>
        public Task<Result> Handle(InboundOrderUpdateCommand request, CancellationToken cancellationToken)
        {
           var result = new Result();
            var info = inboundOrderRepository.GetAll().FirstOrDefault(c => c.InboundOrderId == request.InboundOrderId);
            info.InboundOrderNumber = request.InboundOrderNumber==null||request.InboundOrderNumber.Trim()==""?Guid.NewGuid().ToString():request.InboundOrderNumber;
            info.InboundOrderDate = request.InboundOrderDate;
            info.Remark=request.Remark;
            info.InboundOrderTypeId = request.InboundOrderTypeId;
            info.UpdateId = request.UpdateId;
            info.UpdateDate = DateTime.Now;
            info.UpdateName = request.UpdateName;
            result.Code = inboundOrderRepository.Update(info);
            return Task.FromResult(result);
        }
    }
}
